<?php

use \Illuminate\Support\Facades\DB;





function get_role()
{
    //获取我的角色
    $roleId = DB::table('sy_user_role')
        ->where([
            'user_id'=>get_uid()
        ])
        ->value('role_id');
    return $roleId;
}

/*
 * 获取师傅信息
 * */
function get_area_id()
{
    $areaId = request()->header("area-id");
    return $areaId;
}

/*
 * 获取身份
 * */
function get_identify()
{
    $identify = request()->header("identify");
    $identify = $identify ? (int)$identify : 0;
    return $identify;
}

/*
 * 获取用户信息
 * */
function get_user()
{
    $token = request()->header("Authorization");
    $identify = get_identify() ? (int)get_identify() : 0;
    switch ($identify){
        case 0;
            $user = DB::table('sy_user_token as t')
                ->where('token',$token)
                ->leftJoin('sy_user as u','t.user_id','=','u.id')
                ->select('u.id as uid','u.name','u.guid')
                ->first();
        break;
        case 1;
            $user = DB::table('company_token as t')
                ->where('token',$token)
                ->leftJoin('company as c','t.company_id','=','c.guid')
                ->select('c.id as uid','c.company as name','c.guid')
                ->first();
            break;
    }

    return $user;
}

/*
 * 获取uid
 * */
function get_uid()
{
    return get_user()->uid;
}

/*
 * 获取uid
 * */
function get_uuid()
{
    $user = get_user();
    $guid = null;
    if ($user){
        $guid = $user->guid;
    }
    return $guid;
}


/**
 * 把返回的数据集转换成Tree
 * @param array $list 要转换的数据集
 * @param string $pid parent标记字段
 * @param string $level level标记字段
 * @return array
 */
function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = 'children', $jsonDo = false , $root = 0) {
    // 创建Tree
    $tree = array();
    if (is_array($list) && !is_object($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] = &$list[$key];
        }
        foreach ($list as $key => $data) {
            //转json 数组
            /*if ($jsonDo && isset($data[$jsonDo]) && $data[$jsonDo]){
                $list[$key][$jsonDo] = json_decode($data[$jsonDo],true);
            }*/
            // 判断是否存在parent
            $parentId = $data[$pid];
            if ($root == $parentId) {
                $tree[] = &$list[$key];
            } else {
                if (isset($refer[$parentId])) {
                    $parent             = &$refer[$parentId];
                    $parent['childs'][] = $data['id'];
                    $parent[$child][]   = &$list[$key];
                }
            }
        }
    }
    return $tree;
}


/*
 * 保留数据类型字段
 * */
/*function stayFill( $fill , $param )
{
    $data = [];
    foreach ($param as $key => $item){
        foreach ($fill as $kk => $val){
            if ($key === $val){
                $data[$key] = $item;
            }
        }
    }
    return $data;
}*/

/*
 * 解析 raw Json
 * */
function getRawData()
{
    return json_decode(request()->getContent(),true);
}


/*
 * 获取验证错误信息第一条数据
 * */
//function getFirstErr( $error )
//{
//    $errorMsg = "参数不正确";
//    foreach ($error as $key => $item){
//        $errorMsg = isset($item[0]) ? $item[0] : "参数不正确";
//    }
//    return $errorMsg;
//}

/*
 * 获取日期时间
 * */
function get_datetime()
{
    return date('Y-m-d H:i:s');
}

/*
 * 获取日期
 * */
function get_date()
{
    return date('Y-m-d');
}


/*
 * 获取分类 所有 下级id
 * */
/*function getSubordIds($categorys,$catId=0,$pid="pid",$level=1){
    $subs=array();
    foreach($categorys as $item){
        if($item[$pid]==$catId){
            $item['level']=$level;
            $subs[]=$item['id'];
            $subs=array_merge($subs,getSubordIds($categorys,$item['id'],$pid,$level+1));
        }
    }
    return $subs;
}*/


/*
 * 百分比转换小数点
 * */
function get_ratio_to_point( $ratio )
{
    return $ratio/100;
}

/*
 * 获取自动验收时间
 * */
function get_auto_finish()
{
    $time = DB::table('setting')
        ->where('name','order_auto_settle_timeout_day')
        ->value('value');
    if (!$time){
        $time = 5;
    }

    $time = "+$time day";
    $autoTime = date('Y-m-d H:i:s',strtotime($time,time()));//自动验收时间
    return $autoTime;
}


/*
 * 函数说明：富文本数据进行转换成文本
 *
 * @access  public
 * @param   $content  string  富文本数据
 * @return  string    不包含标签的文本
 */

function contentFormat($content = ''){
    $data = $content;
    $formatData_01 = htmlspecialchars_decode($data);//把一些预定义的 HTML 实体转换为字符
    $formatData_02 = strip_tags($formatData_01);//函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
    return $formatData_02;
}

/*
 * 获取 post
 * */
function get_post( $is_guid=false )
{
    $data = request()->post();
    $data['user_id'] = get_uid();
    if ($is_guid && !isset($data['guid']) ){
        $data['guid'] = getGuid();
    }
    $data['pid'] = isset($data['father_info']) ? get_father_id($data['father_info']) : 0;
    $data['type_id'] = isset($data['type_info']) ? get_father_id($data['type_info']) : 0;
    return $data;
}


/*
 * 获取父级 信息 、
 * @param pid
 * */
function get_father_id( $father , $type="id" )
{
    $fatherId = 0;
    if (!$father || !is_array($father)){
        return $fatherId;
    }
    $fatherId = end($father);
    return  $fatherId;
}


/*
 * 获取上级领导人
 * */
function getParentPersonId()
{
    $parentId = DB::table('sy_user as u')
        ->leftJoin('sy_branch as b','u.branch_id','=','b.id')
        ->select('b.person_id')
        ->where([
            'u.id'=>get_uid()
        ])
        ->value('b.person_id');
    return $parentId;
}

/*
 * 获取全名
 * */
function getFullName( $name , $uid )
{
    return $name . "(".$uid.")";
}

/*
 * 获取流程是否最后一步
 * */
function get_last_flow( $designId )
{
    $lastStep = DB::table('workflow_flow')
        ->select('step')
        ->where('design_id',$designId)
        ->max('step');
    return $lastStep;
}










